#ifndef __SX127X_DRIVER_H__
#define __SX127X_DRIVER_H__
#include "stdbool.h"
#include "stdint.h"
#include "LoRa_model.h"

//定义模块返回状态机（这个是程序用到的，实际模块执行的状态比这个多)
typedef enum
{
    RF_IDLE,	                    //空闲模式
    RF_BUSY,	                    //模块执行任务中
    RF_RX_DONE,	                    //接收完成
    RF_RX_TIMEOUT,	                //接收超时
    RF_TX_DONE,	                    //发送完成
    RF_TX_TIMEOUT,	                //发送超时
    RF_CAD_DETECTED,	            //CAD检测到前导码
	RF_CAD_EMPTY,	            	//CAD检测完成，没有检测到前导码
	RF_CAD_TIMEOUT,	            	//CAD 超时
	RF_UNKNOW_STATUS				//异常状态机
}tRFProcessReturnCodes;

//硬件工作模式
typedef enum{
	LORA_OPMODE_SLEEP=0,
	LORA_OPMODE_STANDBY,
	LORA_OPMODE_SYNTHESIZER_TX,
	LORA_OPMODE_TRANSMITTER,
	LORA_OPMODE_SYNTHESIZER_RX,
	LORA_OPMODE_RECEIVER,
	LORA_OPMODE_RECIVER_SINGLE,
	LORA_OPMODE_CAD,
}LoRaOpModeType;

//驱动结构体，用来保存相关操作函数
typedef struct sRadioDriver
{
    void ( *Init )( lora_config_t *stting );
    void ( *Reset )( void );
    void ( *StartRx )( uint32_t timeoutSystick );
    void ( *GetRxPacket )( void *buffer, uint16_t *size );
    void ( *SetTxPacket )( const void *buffer, uint16_t size,uint32_t timeoutSystick);
		void ( *StartCAD )( void );
    tRFProcessReturnCodes ( *Process )( void );
}tRadioDriver;


extern tRadioDriver g_Radio;

void nf_Setup_SX127x_noname_f(lora_t *lora,lora_msg_cb byte_cb,lora_msg_cb gpio_and_delay);
void sx127xInit(lora_t *lora,lora_config_t*stting);
void Sx127xRestart(lora_t *lora);
void SX127xSetLoRaMode(lora_t *lora);
void Write127xReg(lora_t *lora,uint8_t addr,uint8_t data);
uint8_t Read127xReg(lora_t *lora,uint8_t addr);
void SX127xWriteFifo(lora_t *lora,uint8_t *buffer, uint8_t size );
void SX127xReadFifo( lora_t *lora,uint8_t *buffer, uint8_t size );
void SX127xSetOpMode(lora_t *lora,LoRaOpModeType opMode);
LoRaOpModeType SX127xGetOpMode(lora_t *lora);
void SX127xSetFrf(lora_t *lora,uint32_t fr);
uint8_t sx127xSend(lora_t *lora,uint32_t Fr,uint8_t *data,uint8_t len,uint32_t timeoutMs);
uint8_t sx127xRx(lora_t *lora,uint8_t *buf,uint8_t *len,uint32_t timeoutMs);
uint8_t sx127xRxData(lora_t *lora, uint8_t *buf, uint8_t *len);
uint8_t sx127xStartRx(lora_t *lora,uint32_t Fr);
tRFProcessReturnCodes SX127xProcess(lora_t *lora);

#endif	//end of __SX127X_DRIVER_H__
